package zenkml {
	import core.CoordinatesCal;
	import core.DateFormat;
	import core.Global;
	import core.ScaleRatio;
	import core.TimeFormat;
	import core.XMLOperate;
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import zenkml.core.Draw;
	
	/**
	 * ...
	 * @author Zszen
	 */
	public class ZenKml extends Sprite {
		private var txt_tips:TextField;
		private var txt_result_name:TextField;
		private var txt_result_value:TextField;
		private var com_draw:Draw=new Draw
		public function ZenKml() {
			///area
			graphics.beginFill(0xffffff,0)
			graphics.drawRect(0, 0, Global.stage.stageWidth, Global.stage.stageHeight)
			graphics.endFill()
			///tips
			txt_tips = new TextField()
			txt_tips.appendText("please drag KML file here")
			txt_tips.autoSize = TextFieldAutoSize.LEFT
			txt_tips.selectable = false
			txt_tips.textColor=0xffffff
			txt_tips.filters=[new DropShadowFilter(0,45,0x0,1,2,2,2)]
			addChild(txt_tips)
			///result
			txt_result_name = new TextField()
			txt_result_value = new TextField
			var tf_result:TextFormat = new TextFormat()
			tf_result.size = 18
			tf_result.bold = true
			tf_result.color = 0xFF0080
			tf_result.italic = true
			tf_result.font="arial"
			txt_result_name.defaultTextFormat = tf_result
			tf_result.size = 50
			tf_result.align = TextFormatAlign.RIGHT
			txt_result_value.defaultTextFormat = tf_result
			txt_result_name.appendText("distance:")
			txt_result_name.y = 20
			txt_result_value.x = Global.stage.stageWidth-100
			txt_result_value.y = 80
			txt_result_value.autoSize = TextFieldAutoSize.RIGHT
			addChild(txt_result_name)
			addChild(txt_result_value)
			//
			addChildAt(com_draw,0)
			///listener
			addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,evt_drag_enter)
			addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, evt_drag_drop)
			Global.stage.addEventListener(Event.RESIZE,evt_stageResize)
		}
		/**
		 * 处理文档
		 * @param	file
		 */
		private function dealKMLFile(file:File):void {
			if (file != null) {
				var fs:FileStream = new FileStream()
				fs.open(file,FileMode.READ)
				var xml:XML = new XML(fs.readUTFBytes(fs.bytesAvailable))
				var xo:XMLOperate = new XMLOperate(xml)
				fs.close()
				var coordinatesGroup:Array = []
				var totalTime:Number=0
				var xoRoot:XMLOperate = xo['Document'];
				var oldPosition:Point
				var newPosition:Point
				var oldTime:Date
				var newTime:Date
				var minSpeed:Number = 1
				var maxSpeed:Number = 100//int.MAX_VALUE
				var oldSpeed:Number = 1
				var maxDistance:Number = 0
				for (var i:int = xoRoot.length-1; i >=0 ; i--) {
					try {
						var data:String = xoRoot[i]['Point']['coordinates'].value
						var time:String = xoRoot[i]['name'].value
					}catch (e:Error) {
						continue
					}
					var positionArr:Array = data.split(",")
					newPosition = new Point(positionArr[0], positionArr[1])
					newTime = DateFormat.format(time)
					if (oldTime != null) {
						var drtTime:uint = DateFormat.timeDuring(newTime,oldTime)
						if (drtTime > 0) {
							drtTime = int(drtTime / 1000)
						}else {
							oldTime = newTime
							continue
						}
					}
					oldTime = newTime
					if (oldPosition != null) {
						var distance:Number = CoordinatesCal.GetDistance(newPosition.x, newPosition.y, oldPosition.x, oldPosition.y)//Math.sqrt(Math.pow(newPosition.x - oldPosition.x, 2) + Math.pow(newPosition.y - oldPosition.y, 2))
						var speed:Number = distance / drtTime * 3600
						if (speed >= minSpeed && speed <= maxSpeed) {
							totalTime += drtTime
							maxDistance += distance
							coordinatesGroup.push(newPosition)
						}
					}else {
						coordinatesGroup.push(newPosition)
					}
					oldPosition = newPosition
				}
				com_draw.updateData(coordinatesGroup)
				txt_result_value.text = maxDistance.toFixed(3) + " km"
				evt_stageResize()
			}
		}
		/**
		 * 屏幕变化
		 * @param	e
		 */
		private function evt_stageResize(e:Event = null):void {
			if(com_draw.width==0)return
			com_draw.x = 0
			com_draw.y = 0
			var maxWidth:int = Global.stage.stageWidth - 10
			var maxHeight:int = Global.stage.stageHeight - 10
			ScaleRatio.scaleTo(com_draw, maxWidth, maxHeight, ScaleRatio.Scale_Default)
			var rect:Rectangle = com_draw.getBounds(this)
			com_draw.x = (maxWidth-com_draw.width)/2-rect.x+5
			com_draw.y = (maxHeight-com_draw.height)/2-rect.y+5
		}
		/**
		 * 支持拖拽
		 * @param	e
		 */
		private function evt_drag_enter(e:NativeDragEvent):void {
			NativeDragManager.acceptDragDrop(this)
		}
		/**
		 * 拖拽结束
		 * @param	e
		 */
		private function evt_drag_drop(e:NativeDragEvent):void {
			var dragFile:File = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT)[0] as File
			if (dragFile != null) {
				dealKMLFile(dragFile)
			}
		}
	}
}